import request from '@/http/http'

// 登录参数类型
export interface LoginParams {
  UserName?: string
  Password?: string
  phone?: string
  code?: string// 添加图形验证码字段
  captchaid?: string  //验证码id
  // 添加可能的其他字段，并确保类型正确
  // 如果后端期望某些字段是数字类型，请在这里明确定义
}

// 登录响应中的Data对象类型
export interface LoginData {
  Token: string;
  UserId: number;
  UserName: string;
  RealName?: string; // 根据实际情况，有些字段可能可选
  // ... 可以根据您的实际后端返回添加更多用户信息的字段
}



// 定义完整的后端响应结构
interface ApiResponse<T> {
  Data: T;
  IsSuc: boolean;
  Code: number;
  Msg: string;
}

// 登录方法
export const login = async (data: LoginParams) => {
  return request({
    url:"BaseDataReadAPI/User/Login",
    method:"POST",
    data:{
      "UserName": data.UserName,
      "Password": data.Password,
      "Phone": data.phone,
      "CaptchaId": data.captchaid,
      "Code": data.code
    }
  })
}


// 获取图形验证码方法
export const getCaptcha = async (id?: string) => {
  return request({
    url: "BaseDataReadAPI/User/Captcha",
    method: "GET",
    params: { 
      id: id,
      t: new Date().getTime() // 添加时间戳防止缓存
    },
    responseType: 'blob' // 接收二进制数据
  })
}

// 验证码响应类型
export interface VerificationData {
  CaptchaId: string;   // 验证码ID
  CaptchaImage: string; // 用户输入的验证码值
}

// 验证验证码方法
export const verifyCaptcha = async (data: VerificationData) => {
  return request({
    url: "BaseDataReadAPI/User/Validate2",
    method: "GET",
    params: { 
      id: data.CaptchaId,
      code: data.CaptchaImage,
      t: new Date().getTime() // 添加时间戳防止缓存
    }
  })
}

// 用户列表查询参数类型
export interface UserListParams {
  UserId?: number
  UserName?: string
  Phone?: string
  MembershipLevel?: string
  UserTags?: string
  RegistrationChannel?: string
  IsEnabled?: boolean | null
  RegisterTimeStart?: string
  RegisterTimeEnd?: string
  ConsumptionAmountMin?: number
  ConsumptionAmountMax?: number
  pageIndex?: number
  pageSize?: number
}

// 用户列表项类型
export interface UserItem {
  UserId: number
  UserName: string
  Phone: string
  MembershipLevel: string
  IsEnabled: boolean
  ConsumptionAmount: number
  AvailablePoints: number
  OrderLogs: number
}

// 用户列表响应Data结构
export interface UserListData {
  TotalCount: number;
  TotalPage: number;
  Data: UserItem[];
}

// 获取用户列表方法
export const getUserList = async (params: UserListParams) => {
  return request({
    url: "BaseDataReadAPI/User/GetUserList",
    method: "GET",
    params
  })
}

// 删除参数类型
export interface DeleteParams {
  UserId: number
}

// 删除用户方法
export const deleteUser = async (params: DeleteParams) => {
  return request({
    url: "BaseDataWriteAPI/User/DeleteUser",
    method: "DELETE",
    params
  })
}

// 用户修改参数类型
export interface UpdateUserParams {
  UserId: number
  UserName?: string
  Phone?: string
  MembershipLevel?: string
  Gender?: string
  Birthday?: string | null
  City?: string
  Occupation?: string
  PersonalSignature?: string
  IsEnabled?: boolean
}

// 修改用户方法
export const updateUser = async (data: UpdateUserParams) => {
  return request({
    url: "BaseDataWriteAPI/User/UpdateUser",
    method: "PUT",
    data:data
  })
}

// 用户注册参数类型
export interface RegisterParams {
  UserName: string
  Password: string
  RealName: string
  Phone: string
  Email?: string
  IsEnabled?: boolean
  Gender?: string
  Birthday?: string | Date | null
  City?: string
  Occupation?: string
  PersonalSignature?: string
  MembershipLevel?: string
  UserTags?: string
  RegistrationChannel?: string
}

// 注册用户方法
export const register = async (data: RegisterParams) => {
  return request({
    url: "BaseDataWriteAPI/User/AddUser",
    method: "POST",
    data
  })
} 


//导出用户信息
export const exportUser = async () => {
  return request({
    url: "BaseDataReadAPI/User/ExportUser",
    method: "POST",
    responseType: 'blob', // 指定响应类型为blob
  })
} 
